import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { DeviceMWarn } from "@/api/backend/index";
import { ElNotification } from "element-plus"; //处理导航信息

export const useDeviceWarnSet = defineStore(Names.deviceWarnSet, {
  state: () => {
    return {
      id: 0, //需要查询的设备id
      item: {}, //单个设备的告警列表
      opera: "add",
    };
  },
  getters: {},
  actions: {
    //获取告警配置列表
    getWarnSetList(id: number) {
      return new Promise((resolve, reject) => {
        _get(DeviceMWarn + "/list", { eId: id }, "json", "")
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //新增告警配置
    addWarnSet(data: anyObj) {
      return new Promise((resolve, reject) => {
        _post(DeviceMWarn, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //编辑告警配置
    editWarnSet(data: anyObj) {
      return new Promise((resolve, reject) => {
        _put(DeviceMWarn, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //删除告警配置
    deleteWarnSet(data: anyObj) {
      return new Promise((resolve, reject) => {
        _delete(DeviceMWarn + `/${data.id}`, {}, "json", "")
          .then((res: any) => {
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
